/**
 * ****************************************************************************
 * Project: RoboBridge
 * ----------------------------------------------------------------------------
 * -------------------------- [c]2012 - KrawallMann ---------------------------
 * ---------------------- http://www.krawallmann.de.tc/ -----------------------
 * ****************************************************************************
 * File: AbstractRoboBridgeApplication.java Package:
 * tc.de.krawallmann.robobridge Version: 1.0 Author(s): KrawallMann
 * ****************************************************************************
 * Description:
 * ****************************************************************************
 */
package tc.de.krawallmann.robobridge;

import java.io.IOException;

import tc.de.krawallmann.robobridge.communication.AbstractRemoteCommunicationDevice;
import tc.de.krawallmann.robobridge.communication.DeviceDiscoveryListener;
import tc.de.krawallmann.robobridge.communication.lan.wlan.WLANRemoteDevice;

/**
 * The Class AbstractRoboBridgeApplication provides a basic structure for
 * RoboBridge applications.
 * 
 * @author KrawallMann
 * @version 1.0
 */
public abstract class AbstractRoboBridgeApplication implements Program,
		DeviceDiscoveryListener<WLANRemoteDevice> {

	/** The bridge. */
	protected transient final RoboBridge<WLANRemoteDevice> bridge;
	private final boolean headless;
	private transient RoboView view;

	public AbstractRoboBridgeApplication() throws IOException {
		this(false);
	}

	/**
	 * Instantiates a new simple robo bridge application.
	 * 
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public AbstractRoboBridgeApplication(final boolean headless) throws IOException {
		super();
		bridge = RoboBridgeFactory.createWLANBridge();
		bridge.setProgram(this);
		this.headless = headless;
		if (!headless) {
			view = new RoboView(bridge);
		}
		bridge.getCommunicationInterface().getDeviceScanner().addDeviceDiscoveryListener(this);
		bridge.getCommunicationInterface().startScan();
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.Program#close()
	 */
	@Override
	public void close() {
		// Nothing - override it as you want!
	}

	/**
	 * Gets the bridge.
	 * 
	 * @return the bridge
	 */
	public RoboBridge<WLANRemoteDevice> getBridge() {
		return bridge;
	}

	/**
	 * Gets the view.
	 * 
	 * @return the view
	 */
	public RoboView getView() {
		if (headless) {
			throw new IllegalStateException("RoboView can't be accessed when headless!");
		}
		return view;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * tc.de.krawallmann.robobridge.Program#init(tc.de.krawallmann.robobridge
	 * .RoboBridge)
	 */
	@Override
	public void init(final RoboBridge<? extends AbstractRemoteCommunicationDevice> bridge) {
		// Nothing - override it as you want!
	}

	public boolean isHeadless() {
		return headless;
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.Program#isTerminationRequested()
	 */
	@Override
	public boolean isTerminationRequested() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.communication.DeviceDiscoveryListener#
	 * onDeviceDiscovered
	 * (tc.de.krawallmann.robobridge.communication.AbstractRemoteCommunicationDevice
	 * )
	 */
	@Override
	public void onDeviceDiscovered(final WLANRemoteDevice device) {
		// Nothing - override it as you want!
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.Program#onFirstLoop()
	 */
	@Override
	public void onFirstLoop() {
		// Nothing - override it as you want!
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.Program#update()
	 */
	@Override
	public void update() {
		// Nothing - override it as you want!
	}

}
